package com.lingnan.wanxiangxinxun.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lingnan.wanxiangxinxun.entity.dto.query.UserComment_queryDTO;
import com.lingnan.wanxiangxinxun.entity.po.User;
import com.lingnan.wanxiangxinxun.entity.po.UserComment;
import com.lingnan.wanxiangxinxun.entity.vo.client.UserCommentVO_forNews;
import com.lingnan.wanxiangxinxun.entity.vo.common.PageVO;
import com.lingnan.wanxiangxinxun.mapper.UserCommentMapper;
import com.lingnan.wanxiangxinxun.service.IUserCommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lingnan.wanxiangxinxun.service.IUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 用户评论表 服务实现类
 * </p>
 *
 * @author swl
 * @since 2025-03-13
 */
@Service
public class UserCommentServiceImpl extends ServiceImpl<UserCommentMapper, UserComment> implements IUserCommentService {

    @Autowired
    private IUserService userService;


    //查找所有评论数据
    @Override
    public PageVO<UserComment> getAllInPage(long current, long size) {

        Page<UserComment> p = new Page<>(current, size);
        p.addOrder(OrderItem.desc("create_time"));   // 降序排列

        Page<UserComment> page_MP = this.page(p);
        return PageVO.of(page_MP);
    }

    @Override
    public PageVO<UserComment> getInCondition(UserComment_queryDTO userComment_queryDTO) {
        UserComment userComment = new UserComment();
        BeanUtils.copyProperties(userComment_queryDTO, userComment);
        long current = userComment_queryDTO.getCurrent();
        long size = userComment_queryDTO.getSize();

        Page<UserComment> page = new Page<>(current, size); // 创建分页对象
        Page<UserComment> page_MP = lambdaQuery()
                .eq(userComment.getId() != null, UserComment::getId, userComment.getId())
                .like(userComment.getNewsId() != null, UserComment::getNewsId, userComment.getNewsId())
                .eq(userComment.getUserId() != null, UserComment::getUserId, userComment.getUserId())
                .eq(userComment.getContent() != null, UserComment::getContent, userComment.getContent())
                .between(userComment_queryDTO.getCreateTimeAfter() != null && userComment_queryDTO.getCreateTimeBefore() != null,
                        UserComment::getCreateTime, userComment_queryDTO.getCreateTimeAfter(), userComment_queryDTO.getCreateTimeBefore())

                .orderByDesc(UserComment::getCreateTime)
                .page(page); // 应用分页

        return PageVO.of(page_MP);
    }



    //查找--返回某一条新闻底下的所有评论
    @Override
    public PageVO<UserCommentVO_forNews> getAllByNewsId(Integer newsId, long current, long size) {
        Page<UserComment> page = new Page<>(current, size); // 创建分页对象
        Page<UserComment> page_MP = lambdaQuery()
                .eq(UserComment::getNewsId, newsId)
                .orderByDesc(UserComment::getCreateTime)
                .page(page);

        //返回合适的格式给前端
        PageVO<UserCommentVO_forNews> pageVO = new PageVO<>();
        pageVO.setTotal(page_MP.getTotal());
        pageVO.setPages(page_MP.getPages());

        List<UserCommentVO_forNews> VORecords = new ArrayList<>();
        page_MP.getRecords().forEach(item -> {
            UserCommentVO_forNews ucForNews = new UserCommentVO_forNews();
            BeanUtils.copyProperties(item, ucForNews);

            //根据用户id查找用户信息：用户名字、用户头像地址
            Integer userId = item.getUserId();
            QueryWrapper<User> QW_U = new QueryWrapper<>();
            QW_U.eq("id", userId);
            User user = userService.getOne(QW_U);

            ucForNews.setUserName(user.getName());
            ucForNews.setAvatar(user.getAvatar());
            VORecords.add(ucForNews);
        });
        pageVO.setList(VORecords);
        return pageVO;
    }

    //查找--返回某一个用户的所有评论
    @Override
    public PageVO<UserComment> getAllByUserId(Integer userId, long current, long size) {
        Page<UserComment> page = new Page<>(current, size); // 创建分页对象
        Page<UserComment> page_MP = lambdaQuery()
                .eq(UserComment::getUserId, userId)
                .orderByDesc(UserComment::getCreateTime)
                .page(page);
        return PageVO.of(page_MP);
    }

}
